import {
    Component,
    OnInit,
    ViewChild,
    Injector,
    AfterViewInit,
    ViewEncapsulation,
} from "@angular/core";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { SelectItem } from "primeng/api";
import { AppComponentBase } from "@shared/common/app-component-base";
import { Table } from "primeng/table";
import { Paginator, LazyLoadEvent, FileUpload } from "primeng/primeng";
import {
    WorkingHourServiceProxy,
    LibraryOfLaborServiceProxy,
    EmployeeSalaryDto,
    ProductionOutput,
} from "@shared/service-proxies/service-proxies";
//import { ExcelModalComponent } from '@app/shared/common/excel-modal/excel-modal.component';
import * as moment from "moment";
import { BsDatepickerViewMode } from "ngx-bootstrap/datepicker/models";
import { BsDatepickerConfig, BsDaterangepickerConfig } from "ngx-bootstrap";
import { finalize } from "rxjs/operators";
import * as G2 from "@antv/g2";
import { PrimengTableHelper } from "@shared/helpers/PrimengTableHelper";
import { AppConsts } from "@shared/AppConsts";
import { HttpClient } from "@angular/common/http";

@Component({
    templateUrl: "./library.component.html",
    styleUrls: ["./library.component.css"],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class LibraryComponent extends AppComponentBase {
    @ViewChild("dataTable") dataTable: Table;
    @ViewChild("paginatorNormal") paginatorNormal: Paginator;
    @ViewChild("paginatorSpecial") paginatorSpecial: Paginator;
    @ViewChild("dataTableNormalProductions") dataTableNormalProductions: Table;
    @ViewChild("dataTableSpecialProductions")
    dataTableSpecialProductions: Table;
    @ViewChild("ExcelFileUpload") excelFileUpload: FileUpload;

    advancedFiltersAreShown: Boolean = false;
    uploadUrl: string;

    primengTableHelperNormalProductions = new PrimengTableHelper();
    primengTableHelperSpecialProductions = new PrimengTableHelper();
    paginatorNormalProductions = new Paginator();
    paginatorSpecialProductions = new Paginator();

    filterTaskNo: string = "";
    filterName: string = "";
    filterPicNo: string = "";
    filterModel: string = "";
    filterPart: string = "";

    onlyHaveNotRoutes = false;
    isTemporaryLaborHour = false;

    productionOutputs: ProductionOutput[] = [];

    constructor(
        injector: Injector,
        private _libraryOfLaborServiceProxy: LibraryOfLaborServiceProxy,
        private _httpClient: HttpClient
    ) {
        super(injector);
        this.uploadUrl =
            AppConsts.remoteServiceBaseUrl +
            "/LaborHourLib/ImportLibForLaborHourExcel";
    }
    getNormalProduction(event?: LazyLoadEvent) {
        this.primengTableHelperNormalProductions.showLoadingIndicator();
        if (this.primengTableHelperNormalProductions.shouldResetPaging(event)) {
            this.paginatorNormal.changePage(0);
            return;
        }
        this.primengTableHelperNormalProductions.showLoadingIndicator();
        console.log(this.primengTableHelperNormalProductions.isLoading);
        this._libraryOfLaborServiceProxy
            .getAllNormalProduction(
                this.filterTaskNo,
                this.filterName,
                this.filterPicNo,
                this.filterModel,
                this.filterPart,
                "",
                "",
                this.isTemporaryLaborHour,
                this.onlyHaveNotRoutes,
                "",
                this.primengTableHelperNormalProductions.getSorting(
                    this.dataTableNormalProductions
                ),
                this.primengTableHelperNormalProductions.getMaxResultCount(
                    this.paginatorNormalProductions,
                    event
                ),
                this.primengTableHelperNormalProductions.getSkipCount(
                    this.paginatorNormalProductions,
                    event
                )
            )
            .subscribe((result) => {
                this.primengTableHelperNormalProductions.totalRecordsCount =
                    result.totalCount;
                this.primengTableHelperNormalProductions.records = result.items;
                this.primengTableHelperNormalProductions.hideLoadingIndicator();
            });
    }

    getSpecialProduction(event?: LazyLoadEvent) {
        if (
            this.primengTableHelperSpecialProductions.shouldResetPaging(event)
        ) {
            this.paginatorSpecial.changePage(0);

            return;
        }
        this.primengTableHelperSpecialProductions.showLoadingIndicator();

        this._libraryOfLaborServiceProxy
            .getAllSpecialProduction(
                this.filterTaskNo,
                this.filterName,
                this.filterPicNo,
                this.filterModel,
                this.filterPart,
                "",
                "",
                this.isTemporaryLaborHour,
                this.onlyHaveNotRoutes,

                "",
                this.primengTableHelperSpecialProductions.getSorting(
                    this.dataTableSpecialProductions
                ),
                this.primengTableHelperSpecialProductions.getMaxResultCount(
                    this.paginatorSpecialProductions,
                    event
                ),
                this.primengTableHelperSpecialProductions.getSkipCount(
                    this.paginatorSpecialProductions,
                    event
                )
            )
            .subscribe((result) => {
                this.primengTableHelperSpecialProductions.totalRecordsCount =
                    result.totalCount;
                this.primengTableHelperSpecialProductions.records =
                    result.items;
                this.primengTableHelperSpecialProductions.hideLoadingIndicator();
            });
    }

    reloadPage(): void {
        this.paginatorNormal.changePage(this.paginatorNormal.getPage());
        this.paginatorSpecial.changePage(this.paginatorSpecial.getPage());
    }

    deleteProduction(production: ProductionOutput): void {
        this.message.confirm(
            "删除【" +
                production.id +
                "】,图号：" +
                production.picNo +
                "的零件定额",
            this.l("AreYouSure"),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._libraryOfLaborServiceProxy
                        .deleteProduction(production.id)
                        .subscribe(() => {
                            this.reloadPage();
                            this.notify.success(this.l("SuccessfullyDeleted"));
                        });
                }
            }
        );
    }

    uploadExcel(data: { files: File }): void {
        this.primengTableHelperNormalProductions.showLoadingIndicator();

        const formData: FormData = new FormData();
        const file = data.files[0];
        formData.append("file", file, file.name);
        this._httpClient
            .post<any>(this.uploadUrl, formData)
            .pipe(finalize(() => this.excelFileUpload.clear()))
            .subscribe((response) => {
                this.primengTableHelperNormalProductions.hideLoadingIndicator();

                if (response.success) {
                    this.message.success("导入Excel成功");

                    //this.importSalaryDtoList.salaryList = this.salary;
                } else if (response.error != null) {
                    this.message.error("导入Excel失败");
                }
            });
    }

    onUploadExcelError(): void {
        this.primengTableHelperNormalProductions.hideLoadingIndicator();
        this.notify.error("上传失败");
    }
}
